package com.kikyou.service.impl;

import com.kikyou.pojo.Department;
import com.kikyou.mapper.DepartmentMapper;
import com.kikyou.pojo.RespBean;
import com.kikyou.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuankaiqiang
 * @since 2022-03-08
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired(required = false)
    private DepartmentMapper departmentMapper;
    @Autowired
    private  OplogServiceImpl oplogServiceImpl;
    /**
     * Description : TODO  获取组织架构<br/>
     * ChangeLog : 1. 创建 (2022/3/8 11:46 [ykq11]);
     * *
     * @return java.util.List<com.kikyou.pojo.Department>
     **/
    @Override
    public List<Department> getAllDepartment() {
        return departmentMapper.getAllDepartment(1);
    }

    /**
     * Description : TODO 添加部门 <br/>
     * ChangeLog : 1. 创建 (2022/3/8 18:19 [ykq11]);
     ** @param dep
     * @return com.kikyou.pojo.RespBean
     **/
    @Override
    public RespBean addDep(Department dep) {
        dep.setEnabled(true);
        dep.setDeleted(true);
        departmentMapper.addDep(dep);
        if (1 == dep.getResult()) {
            oplogServiceImpl.insertMessage("增加"+dep.getOrganizationName()+"部门成功");
            return RespBean.success("添加成功！",dep);
        }
        return RespBean.error("添加失败!");
    }
    /**
     * 删除部门
     * @param id
     * @return
     */
    @Override
    public RespBean deleteDep(Integer id) {
        Department dep = new Department();
        dep.setId(id);
        departmentMapper.deleteDep(dep);
        if (-2 == dep.getResult()) {
            return RespBean.error("该部门下含有子部门，删除失败！");
        }
        if (-1 == dep.getResult()) {
            return RespBean.error("该部门下含有员工，删除失败！");
        }
        if (1 == dep.getResult()) {
            oplogServiceImpl.insertMessage("删除"+dep.getOrganizationName()+"部门成功");
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }
}
